package test.com.sms.selenium.singlepoint.util;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.RemoteWebElement;

import test.com.sms.selenium.framework.SeleniumPageUtil;
import test.com.sms.selenium.util.SeleniumUtil;

public class SpTicketListPageUtil  extends SeleniumPageUtil{
	
	public final static By LIST_LBL_HEADER = By.id("ctl44_IncidentsReport_Header_lblHeader");
	public final static By LIST_BUTTON_EXPORT = By.id("ctl44_IncidentsReport_Header_imgExcelExport");
	
	public final static By LIST_LINK_SCOPE = By.xpath("//*[@id='7464fd4c-2094-42ad-acd5-40b4ac5e17ed']/a");
	
	// scope elements
	public final static By LIST_LI_SCOPE_CUSTOMER_INCIDENTS = By.name("Customer Incidents");
	public final static By LIST_LI_SCOPE_LAST_24_HOURS = By.name("Last 24 Hours");
	public final static By LIST_LI_SCOPE_MY_WAREHOUSES = By.name("My Warehouses");
	public final static By LIST_LI_SCOPE_MY_FE_TEAM = By.name("My FE Team");
	public final static By LIST_LI_SCOPE_MY_PEERS = By.name("My Peers");
	public final static By LIST_LI_SCOPE_WAREHOUSE_BY_CUSTOMER = By.name("My Warehouse by Customer");
	public final static By LIST_LI_SCOPE_ALL_INCIDENTS = By.name("All Incidents");
	
	public final static By LIST_LBL_PAGE = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr/td[1]");
	public final static By LIST_LBL_PAGE_NO = By.id("ctl44_IncidentsReport_PagingTop_lblPagingPageNumber");
	public final static By LIST_LBL_PAGE_OF = By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr/td[3]");
	public final static By LIST_LBL_PAGE_COUNT = By.id("ctl44_IncidentsReport_PagingTop_lblPagingPageCount");
	public final static By LIST_BUTTON_FIRST = By.id("ctl44_IncidentsReport_PagingTop_imgFirst");
	public final static By LIST_BUTTON_PREVIOUS = By.id("ctl44_IncidentsReport_PagingTop_imgPrevious");
	public final static By LIST_BUTTON_NEXT = By.id("ctl44_IncidentsReport_PagingTop_imgNext");
	public final static By LIST_BUTTON_LAST = By.id("ctl44_IncidentsReport_PagingTop_imgLast");
	public final static By LIST_LBL_INCIDENT_ID = By.id("ctl44_IncidentsReport_sort_1");
	public final static By LIST_LBL_CUSTOMER = By.id("ctl44_IncidentsReport_sort_2");
	public final static By LIST_LBL_INCIDNET_DATE = By.id("ctl44_IncidentsReport_sort_3");
	public final static By LIST_LBL_STATUS = By.id("ctl44_IncidentsReport_sort_4");
	public final static By LIST_LBL_SERIAL_NO = By.id("ctl44_IncidentsReport_sort_5");
	public final static By LIST_LBL_MODEL = By.id("ctl44_IncidentsReport_sort_6");
	public final static By LIST_LBL_SRO_ID = By.id("ctl44_IncidentsReport_sort_7");
	public final static By LIST_LBL_SRO_TYPE = By.id("ctl44_IncidentsReport_sort_8");
	public final static By LIST_LBL_DESCRIPTION = By.id("ctl44_IncidentsReport_sort_9");
	public final static By LIST_LBL_WAREHOUSE = By.id("ctl44_IncidentsReport_sort_10");
	public final static By LIST_LBL_CONTACT = By.id("ctl44_IncidentsReport_sort_11");
	public final static By LIST_LBL_REQUESTOR = By.id("ctl44_IncidentsReport_sort_12");
	
	// result list span
	public final static By LIST_SPAN_RESULT = By.id("ctl44_IncidentsReport_ReportOutput");
	
	// 0. default list elements
	public final static By LIST_IMG_INCIDENT_ID_FILTER = By.id("ctl44_IncidentsReport_filter_1");
	public final static By LIST_SPAN_INCIDENT_ID = By.id("ctl44_IncidentsReport_sort_1");
	public final static By LIST_IMG_CUSTOMER_FILTER = By.id("ctl44_IncidentsReport_filter_2");
	public final static By LIST_SPAN_CUSTOMER = By.id("ctl44_IncidentsReport_sort_2");
	public final static By LIST_IMG_INCIDENT_DATE_FILTER = By.id("ctl44_IncidentsReport_filter_3");
	public final static By LIST_SPAN_INCIDNET_DATE = By.id("ctl44_IncidentsReport_sort_3");
	public final static By LIST_IMG_STATUS_FILTER = By.id("ctl44_IncidentsReport_filter_4");
	public final static By LIST_SPAN_STATUS = By.id("ctl44_IncidentsReport_sort_4");
	public final static By LIST_IMG_SERIAL_NO_FILTER = By.id("ctl44_IncidentsReport_filter_5");
	public final static By LIST_SPAN_SERIAL_NO = By.id("ctl44_IncidentsReport_sort_5");
	public final static By LIST_IMG_MODEL_FILTER = By.id("ctl44_IncidentsReport_filter_6");
	public final static By LIST_SPAN_MODEL = By.id("ctl44_IncidentsReport_sort_6");
	public final static By LIST_IMG_SRO_ID_FILTER = By.id("ctl44_IncidentsReport_filter_7");
	public final static By LIST_SPAN_SRO_ID = By.id("ctl44_IncidentsReport_sort_7");
	public final static By LIST_IMG_SRO_TYPE_FILTER = By.id("ctl44_IncidentsReport_filter_8");
	public final static By LIST_SPAN_SRO_TYPE = By.id("ctl44_IncidentsReport_sort_8");
	public final static By LIST_IMG_DESCRIPTION_FILTER = By.id("ctl44_IncidentsReport_filter_9");
	public final static By LIST_SPAN_DESCRIPTION = By.id("ctl44_IncidentsReport_sort_9");
	public final static By LIST_IMG_WAREHOUSE_FILTER = By.id("ctl44_IncidentsReport_filter_10");
	public final static By LIST_SPAN_WAREHOUSE = By.id("ctl44_IncidentsReport_sort_10");
	public final static By LIST_IMG_CONTACT_FILTER = By.id("ctl44_IncidentsReport_filter_11");
	public final static By LIST_SPAN_CONTACT = By.id("ctl44_IncidentsReport_sort_11");
	public final static By LIST_IMG_REQUESTOR_FILTER = By.id("ctl44_IncidentsReport_filter_12");
	public final static By LIST_SPAN_REQUESTOR = By.id("ctl44_IncidentsReport_sort_12");
	
	// 1. Customer Incidents elements
	public final static By LIST_SPAN_CI_INCIDENT_ID = By.id("ctl44_IncidentsReport_sort_1");
	public final static By LIST_SPAN_CI_STATUS = By.id("ctl44_IncidentsReport_sort_2");
	public final static By LIST_SPAN_CI_INCIDENT_DATE = By.id("ctl44_IncidentsReport_sort_3");
	public final static By LIST_SPAN_CI_PRIORITY = By.id("ctl44_IncidentsReport_sort_4");
	public final static By LIST_SPAN_CI_DESCRIPTION = By.id("ctl44_IncidentsReport_sort_5");
	public final static By LIST_SPAN_CI_CONTRACT_NUMBER = By.id("ctl44_IncidentsReport_sort_6");
	public final static By LIST_SPAN_CI_CONTRACT_LINE = By.id("ctl44_IncidentsReport_sort_7");
	public final static By LIST_SPAN_CI_ASSET = By.id("ctl44_IncidentsReport_sort_8");
	public final static By LIST_SPAN_CI_SERIAL_NUMBER = By.id("ctl44_IncidentsReport_sort_9");
	public final static By LIST_SPAN_CI_REQUESTER = By.id("ctl44_IncidentsReport_sort_10");
	public final static By LIST_SPAN_CI_CUSTOMER_TICKET_NUMBER = By.id("ctl44_IncidentsReport_sort_11");
	public final static By LIST_SPAN_CI_SMS = By.id("ctl44_IncidentsReport_sort_12");
	
	// 2. Last 24 Hours elements
	public final static By LIST_SPAN_L24_INCIDENT_ID = By.id("ctl44_IncidentsReport_sort_1");
	public final static By LIST_SPAN_L24_CUSTOMER = By.id("ctl44_IncidentsReport_sort_2");
	public final static By LIST_SPAN_L24_INCIDENT_DATE = By.id("ctl44_IncidentsReport_sort_3");
	public final static By LIST_SPAN_L24_STATUS = By.id("ctl44_IncidentsReport_sort_4");
	public final static By LIST_SPAN_L24_ASSIGNED_TO = By.id("ctl44_IncidentsReport_sort_5");
	public final static By LIST_SPAN_L24_SERIAL_NUMBER = By.id("ctl44_IncidentsReport_sort_6");
	public final static By LIST_SPAN_L24_MODEL = By.id("ctl44_IncidentsReport_sort_7");
	public final static By LIST_SPAN_L24_SRO_ID = By.id("ctl44_IncidentsReport_sort_8");
	public final static By LIST_SPAN_L24_SRO_TYPE = By.id("ctl44_IncidentsReport_sort_9");
	public final static By LIST_SPAN_L24_DESCRIPTION = By.id("ctl44_IncidentsReport_sort_10");
	public final static By LIST_SPAN_L24_WAREHOUSE = By.id("ctl44_IncidentsReport_sort_11");
	public final static By LIST_SPAN_L24_CONTACT = By.id("ctl44_IncidentsReport_sort_12");
	public final static By LIST_SPAN_L24_REQUESTOR = By.id("ctl44_IncidentsReport_sort_13 ");
	
	// 3. My Warehouses elements
	public final static By LIST_SPAN_MW_INCIDENT_ID = By.id("ctl44_IncidentsReport_sort_1");
	public final static By LIST_SPAN_MW_CUSTOMER = By.id("ctl44_IncidentsReport_sort_2");
	public final static By LIST_SPAN_MW_INCIDENT_DATE = By.id("ctl44_IncidentsReport_sort_3");
	public final static By LIST_SPAN_MW_STATUS = By.id("ctl44_IncidentsReport_sort_4");
	public final static By LIST_SPAN_MW_ASSIGNED_TO = By.id("ctl44_IncidentsReport_sort_4");
	public final static By LIST_SPAN_MW_SERIAL_NUMBER = By.id("ctl44_IncidentsReport_sort_5");
	public final static By LIST_SPAN_MW_MODEL = By.id("ctl44_IncidentsReport_sort_6");
	public final static By LIST_SPAN_MW_SRO_ID = By.id("ctl44_IncidentsReport_sort_7");
	public final static By LIST_SPAN_MW_SRO_TYPE = By.id("ctl44_IncidentsReport_sort_8");
	public final static By LIST_SPAN_MW_DESCRIPTION = By.id("ctl44_IncidentsReport_sort_9");
	public final static By LIST_SPAN_MW_CONTACT = By.id("ctl44_IncidentsReport_sort_11");
	public final static By LIST_SPAN_MW_REQUESTOR = By.id("ctl44_IncidentsReport_sort_12");
	
	// 4. My FE Team
	public final static By LIST_SPAN_MF_INCIDENT_ID = By.id("ctl44_IncidentsReport_sort_1");
	public final static By LIST_SPAN_MF_CUSTOMER = By.id("ctl44_IncidentsReport_sort_2");
	public final static By LIST_SPAN_MF_INCIDENT_DATE = By.id("ctl44_IncidentsReport_sort_3");
	public final static By LIST_SPAN_MF_STATUS = By.id("ctl44_IncidentsReport_sort_4");
	public final static By LIST_SPAN_MF_SERIAL_NUMBER = By.id("ctl44_IncidentsReport_sort_5");
	public final static By LIST_SPAN_MF_MODEL = By.id("ctl44_IncidentsReport_sort_6");
	public final static By LIST_SPAN_MF_SRO_ID = By.id("ctl44_IncidentsReport_sort_7");
	public final static By LIST_SPAN_MF_SRO_TYPE = By.id("ctl44_IncidentsReport_sort_8");
	public final static By LIST_SPAN_MF_DESCRIPTION = By.id("ctl44_IncidentsReport_sort_9");
	public final static By LIST_SPAN_MF_WAREHOUSE = By.id("ctl44_IncidentsReport_sort_10");
	public final static By LIST_SPAN_MF_CONTACT = By.id("ctl44_IncidentsReport_sort_11");
	public final static By LIST_SPAN_MF_REQUESTOR = By.id("ctl44_IncidentsReport_sort_12");
	
	// 5. My Peers
	public final static By LIST_SPAN_MP_INCIDENT_ID = By.id("ctl44_IncidentsReport_sort_1");
	public final static By LIST_SPAN_MP_CUSTOMER = By.id("ctl44_IncidentsReport_sort_2");
	public final static By LIST_SPAN_MP_INCIDENT_DATE = By.id("ctl44_IncidentsReport_sort_3");
	public final static By LIST_SPAN_MP_STATUS = By.id("ctl44_IncidentsReport_sort_4");
	public final static By LIST_SPAN_MP_SERIAL_NUMBER = By.id("ctl44_IncidentsReport_sort_5");
	public final static By LIST_SPAN_MP_MODEL = By.id("ctl44_IncidentsReport_sort_6");
	public final static By LIST_SPAN_MP_SRO_ID = By.id("ctl44_IncidentsReport_sort_7");
	public final static By LIST_SPAN_MP_SRO_TYPE = By.id("ctl44_IncidentsReport_sort_8");
	public final static By LIST_SPAN_MP_DESCRIPTION = By.id("ctl44_IncidentsReport_sort_9");
	public final static By LIST_SPAN_MP_WAREHOUSE = By.id("ctl44_IncidentsReport_sort_10");
	public final static By LIST_SPAN_MP_CONTACT = By.id("ctl44_IncidentsReport_sort_11");
	public final static By LIST_SPAN_MP_REQUESTOR = By.id("ctl44_IncidentsReport_sort_12");
	
	// 6. My Warehouse by Customer
	public final static By LIST_SPAN_MWB_INCIDENT_ID = By.id("ctl44_IncidentsReport_sort_1");
	// ??? web site may has problem here public final static By LIST_SPAN_MWB_CUSTOMER = By.id("ctl44_IncidentsReport_sort_2");
	public final static By LIST_SPAN_MWB_INCIDENT_DATE = By.id("ctl44_IncidentsReport_sort_2");
	public final static By LIST_SPAN_MWB_STATUS = By.id("ctl44_IncidentsReport_sort_3");
	// ??? web site may has problem here
	public final static By LIST_SPAN_MWB_STATUS2 = By.id("ctl44_IncidentsReport_sort_4");
	public final static By LIST_SPAN_MWB_SERIAL_NUMBER = By.id("ctl44_IncidentsReport_sort_5");
	public final static By LIST_SPAN_MWB_MODEL = By.id("ctl44_IncidentsReport_sort_6");
	public final static By LIST_SPAN_MWB_SRO_ID = By.id("ctl44_IncidentsReport_sort_7");
	public final static By LIST_SPAN_MWB_SRO_TYPE = By.id("ctl44_IncidentsReport_sort_8");
	public final static By LIST_SPAN_MWB_DESCRIPTION = By.id("ctl44_IncidentsReport_sort_9");
	public final static By LIST_SPAN_MWB_WAREHOUSE = By.id("ctl44_IncidentsReport_sort_10");
	public final static By LIST_SPAN_MWB_CONTACT = By.id("ctl44_IncidentsReport_sort_11");
	public final static By LIST_SPAN_MWB_REQUESTOR = By.id("ctl44_IncidentsReport_sort_12");
	
	// 7. ALL incidents
	public final static By LIST_SPAN_ALL_INCIDENT_ID = By.id("ctl44_IncidentsReport_sort_1");
	public final static By LIST_SPAN_ALL_CUSTOMER = By.id("ctl44_IncidentsReport_sort_2");
	public final static By LIST_SPAN_ALL_INCIDENT_DATE = By.id("ctl44_IncidentsReport_sort_3");
	public final static By LIST_SPAN_ALL_STATUS = By.id("ctl44_IncidentsReport_sort_4");
	public final static By LIST_SPAN_ALL_ASSIGNED_TO = By.id("ctl44_IncidentsReport_sort_5");
	public final static By LIST_SPAN_ALL_SERIAL_NUMBER = By.id("ctl44_IncidentsReport_sort_6");
	public final static By LIST_SPAN_ALL_MODEL = By.id("ctl44_IncidentsReport_sort_7");
	public final static By LIST_SPAN_ALL_SRO_ID = By.id("ctl44_IncidentsReport_sort_8");
	public final static By LIST_SPAN_ALL_SRO_TYPE = By.id("ctl44_IncidentsReport_sort_9");
	public final static By LIST_SPAN_ALL_DESCRIPTION = By.id("ctl44_IncidentsReport_sort_10");
	public final static By LIST_SPAN_ALL_WAREHOUSE = By.id("ctl44_IncidentsReport_sort_11");
	public final static By LIST_SPAN_ALL_CONTACT = By.id("ctl44_IncidentsReport_sort_12");
	public final static By LIST_SPAN_ALL_REQUESTOR = By.id("ctl44_IncidentsReport_sort_13");
	
	public final static By LIST_LINK_ALL_INCIDENTS = By.xpath("//*[@id='ctl44_IncidentsReport_ReportOutput']/table/tbody/tr/td[1]/a");
	public final static By LIST_IMG_REFRESH = By.id("ctl44_IncidentsReport_Header_imgRefresh");
	
	public static void enterListPage(){
		SpHomePageUtil.enter();
		SpHomePageUtil.enterListPage();
	}
	
	public static void waitForPageToLoad() {
		// list page in content area
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		SeleniumUtil.waitOnElements(new By[]{
				LIST_LINK_SCOPE, LIST_BUTTON_EXPORT, LIST_BUTTON_FIRST, LIST_BUTTON_PREVIOUS, LIST_BUTTON_NEXT, LIST_BUTTON_LAST
			});
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(LIST_LINK_SCOPE));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(LIST_BUTTON_EXPORT));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(LIST_BUTTON_FIRST));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(LIST_BUTTON_PREVIOUS));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(LIST_BUTTON_NEXT));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(LIST_BUTTON_LAST));
	}
	
	/** wait for result list to show complete*/
	public static void waitForResultListToLoadComplete(){
			
		final int WAIT_FOR_ROUND = 6;
		final int WAIT_FOR_MILLISECONDS = 500;
		
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		// if result list shows, wait for all records to load
		long prevCount = 0;
		int stableRound = 0;
		while(true){
			long beforeFind = System.currentTimeMillis();
			long rowCount = SeleniumUtil.findElements(SeleniumUtil.getCachedElement(LIST_SPAN_RESULT), By.xpath(".//a")).size();
			long endFind = System.currentTimeMillis();
			
			// make sure not wait too long when really no result exists
			if(endFind - beforeFind >= WAIT_FOR_ROUND * WAIT_FOR_MILLISECONDS) {
				break;
			}
			
			// if record count becomes stable, quit wait, else continue 
			if(rowCount == prevCount){
				stableRound ++;
				if(stableRound == WAIT_FOR_ROUND) break;
			} else {
				stableRound = 0;
			}
			prevCount = rowCount;
			SeleniumUtil.waitForFixedMilliseconds(WAIT_FOR_MILLISECONDS);
		}
	}
	public static void displayHidenMenu(By by){
		try {
			RemoteWebElement element = (RemoteWebElement)SeleniumUtil.getCachedElement(by);
			WebDriver driver = element.getWrappedDriver(); 
			Actions action = new Actions(driver);
			action.moveToElement(element);
			action.perform();
		} catch (Exception e) {
			SeleniumUtil.getScreenShot(e);
		}
	}
}
